import axios from "axios";
import Cookies from "js-cookie";
import ElementUI from "element-ui";
import Store from "@/store";
import utils from "@/utils";

const http = axios.create({
  baseURL: "/nongmao",
  withCredentials: true,
  timeout: 50000, // request timeout
});

let loadingIns;
let clearLoading;
// 拦截请求
http.interceptors.request.use(
  function (config) {
    if (Cookies.get("X-MINESPAN-SESSION")) {
      config.headers["X-MINESPAN-SESSION"] = Cookies.get("X-MINESPAN-SESSION");
    }

    if (config.withLoading) {
      loadingIns = utils.LoadingIns()
    }

    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

// 拦截响应
http.interceptors.response.use(
  (response) => {
    if (response.status === 200) {
      // 移除loading
      clearTimeout(clearLoading)
      clearLoading = setTimeout(() => {
        loadingIns && loadingIns.close();
      }, 500);

      // 放行白名单的响应
      if(response.config.noInterceptors === true){
        return Promise.resolve(response);
      }

      // 正常请示
      if (response.data.success) {
        return Promise.resolve(response.data);
      } else {
        if(response.data.msgCode == "common.starter.eduservice.needLogin"){
          Store.dispatch('LOGOUT')

          return Promise.reject(response);
        }

        ElementUI.Message({
          type: "error",
          message: response.data.prompt,
        });
        return Promise.reject(response);
      }

      switch (response.data.code) {
        // 401: 未登录
        // 未登录则跳转登录页面，并携带当前页面的路径
        // 在登录成功后返回当前页面，这一步需要在登录页操作。
        //case 404:
        /* Toast({
              message: '用户名密码错误',
              duration: 1000,
              forbidClick: true
          }); */
        //alert("用户名密码错误")
        //router.replace({
        //path: '/login',
        //query: { redirect: router.currentRoute.fullPath }
        //});
        //break;
        // 403 token过期
        // 登录过期对用户进行提示
        // 清除本地token和清空vuex中token对象
        // 跳转登录页面
        /* case 403:
          Toast({
              message: '登录过期，请重新登录',
              duration: 1000,
              forbidClick: true
          });
          // 清除token
          localStorage.removeItem('token');
          store.commit('loginSuccess', null);
          // 跳转登录页面，并将要浏览的页面fullPath传过去，登录成功后跳转需要访问的页面
          setTimeout(() => {
              router.replace({
                  path: '/login',
                  query: {
                      redirect: router.currentRoute.fullPath
                  }
              });
          }, 1000);
          break; */
        // 404请求不存在
        case 400:
          /* Toast({
          message: '参数请求不正确',
          duration: 1500,
          forbidClick: true
        }); */
          console.log("参数请求不正确");
          break;
        case 500:
          /* Toast({
            message: '查询异常',
            duration: 1500,
            forbidClick: true
        }); */
          console.log("查询异常");
          break;
        // 其他错误，直接抛出错误提示
        default:
          this.$message(msg);
          /* Toast({
            message: error.response.data.message,
            duration: 1500,
            forbidClick: true
        }); */
          console.log(response.data.msg);
      }
      return Promise.resolve(response);
    } else {
      return Promise.reject(response);
    }
  },
  (error) => {
    loadingIns && loadingIns.close();
    return Promise.reject(error);
  }
);

export default http;
